package f;

import f.f0.c;
import g.e;
import g.f;
import g.h;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.annotation.Nullable;

/* compiled from: MultipartBody */
public final class u extends z {
    public static final t e = t.a("multipart/mixed");
    public static final t f = t.a("multipart/form-data");
    public static final byte[] g = new byte[]{(byte) 58, (byte) 32};
    public static final byte[] h = new byte[]{(byte) 13, (byte) 10};
    public static final byte[] i = new byte[]{(byte) 45, (byte) 45};
    public final h a;
    public final t b;
    public final List<b> c;
    public long d = -1;

    /* compiled from: MultipartBody */
    public static final class a {
        public final h a;
        public t b = u.e;
        public final List<b> c = new ArrayList();

        public a() {
            String uuid = UUID.randomUUID().toString();
            this.a = h.e(uuid);
        }
    }

    /* compiled from: MultipartBody */
    public static final class b {
        @Nullable
        public final q a;
        public final z b;

        public b(@Nullable q qVar, z zVar) {
            this.a = qVar;
            this.b = zVar;
        }

        public static b a(String str, @Nullable String str2, z zVar) {
            Objects.requireNonNull(str, "name == null");
            StringBuilder stringBuilder = new StringBuilder("form-data; name=");
            u.a(stringBuilder, str);
            if (str2 != null) {
                stringBuilder.append("; filename=");
                u.a(stringBuilder, str2);
            }
            r2 = new String[2];
            int i = 0;
            r2[0] = "Content-Disposition";
            r2[1] = stringBuilder.toString();
            r2 = (String[]) r2.clone();
            int i2 = 0;
            while (i2 < r2.length) {
                if (r2[i2] != null) {
                    r2[i2] = r2[i2].trim();
                    i2++;
                } else {
                    throw new IllegalArgumentException("Headers cannot be null");
                }
            }
            while (i < r2.length) {
                str2 = r2[i];
                String str3 = r2[i + 1];
                q.a(str2);
                q.b(str3, str2);
                i += 2;
            }
            q qVar = new q(r2);
            Objects.requireNonNull(zVar, "body == null");
            if (qVar.c("Content-Type") != null) {
                throw new IllegalArgumentException("Unexpected header: Content-Type");
            } else if (qVar.c("Content-Length") == null) {
                return new b(qVar, zVar);
            } else {
                throw new IllegalArgumentException("Unexpected header: Content-Length");
            }
        }
    }

    static {
        t.a("multipart/alternative");
        t.a("multipart/digest");
        t.a("multipart/parallel");
    }

    public u(h hVar, t tVar, List<b> list) {
        this.a = hVar;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(tVar);
        stringBuilder.append("; boundary=");
        stringBuilder.append(hVar.w());
        this.b = t.a(stringBuilder.toString());
        this.c = c.p(list);
    }

    public static StringBuilder a(StringBuilder stringBuilder, String str) {
        stringBuilder.append('\"');
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char charAt = str.charAt(i);
            if (charAt == 10) {
                stringBuilder.append("%0A");
            } else if (charAt == 13) {
                stringBuilder.append("%0D");
            } else if (charAt != '\"') {
                stringBuilder.append(charAt);
            } else {
                stringBuilder.append("%22");
            }
        }
        stringBuilder.append('\"');
        return stringBuilder;
    }

    public final long b(@Nullable f fVar, boolean z) throws IOException {
        e eVar;
        if (z) {
            fVar = new e();
            eVar = fVar;
        } else {
            eVar = null;
        }
        int size = this.c.size();
        long j = 0;
        for (int i = 0; i < size; i++) {
            b bVar = (b) this.c.get(i);
            q qVar = bVar.a;
            z zVar = bVar.b;
            fVar.V(i);
            fVar.W(this.a);
            fVar.V(h);
            if (qVar != null) {
                int f = qVar.f();
                for (int i2 = 0; i2 < f; i2++) {
                    fVar.j0(qVar.d(i2)).V(g).j0(qVar.g(i2)).V(h);
                }
            }
            t contentType = zVar.contentType();
            if (contentType != null) {
                fVar.j0("Content-Type: ").j0(contentType.a).V(h);
            }
            long contentLength = zVar.contentLength();
            if (contentLength != -1) {
                fVar.j0("Content-Length: ").k0(contentLength).V(h);
            } else if (z) {
                eVar.n();
                return -1;
            }
            byte[] bArr = h;
            fVar.V(bArr);
            if (z) {
                j += contentLength;
            } else {
                zVar.writeTo(fVar);
            }
            fVar.V(bArr);
        }
        byte[] bArr2 = i;
        fVar.V(bArr2);
        fVar.W(this.a);
        fVar.V(bArr2);
        fVar.V(h);
        if (z) {
            j += eVar.b;
            eVar.n();
        }
        return j;
    }

    public long contentLength() throws IOException {
        long j = this.d;
        if (j != -1) {
            return j;
        }
        j = b(null, true);
        this.d = j;
        return j;
    }

    public t contentType() {
        return this.b;
    }

    public void writeTo(f fVar) throws IOException {
        b(fVar, false);
    }
}
